package com.tang.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.tang.dto.CategoryDto;
import com.tang.enums.AppHttpCodeEnum;
import com.tang.sys.ResponseResult;
import com.tang.sys.entity.Article;
import com.tang.sys.entity.Category;
import com.tang.sys.service.ICategoryService;
import com.tang.sys.vo.CategoryVo;
import com.tang.sys.vo.ExcelCategoryVo;
import com.tang.sys.vo.PageVo;
import com.tang.utils.BeanCopyUtils;
import com.tang.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class AdminCategoryController {

    @Autowired
    private ICategoryService iCategoryService;

    //查询所有分类的标签
    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory(){
        List<CategoryVo> list = iCategoryService.listAllCategory();
        return ResponseResult.okResult(list);
    }

    @GetMapping("/list")
    public ResponseResult<PageVo> list (Integer pageNum,Integer pageSize,CategoryDto categoryDto){

        return iCategoryService.pageCategoryList(pageNum,pageSize,categoryDto);
    }

    //新增
    @PostMapping
    public ResponseResult<CategoryVo> addCategory(@RequestBody CategoryVo categoryVo){

        return iCategoryService.addCategory(categoryVo);
    }

    //删除分类
    @DeleteMapping("{id}")
    public ResponseResult deleteCategory(@PathVariable("id") Long id){

        return iCategoryService.deleteCategory(id);
    }

    //修改分类
    @GetMapping("{id}")
    public ResponseResult getInfoCategory(@PathVariable("id") Long id){

        return iCategoryService.getInfoCategory(id);
    }
    //更新分类
    @PutMapping
    public ResponseResult<CategoryVo> updateCategory(@RequestBody CategoryVo categoryVo){

        return iCategoryService.updateCategory(categoryVo);

    }
    //导出execl
    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx",response);
            //获取需要导出的数据
            List<Category> categoryVos = iCategoryService.list();

            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(categoryVos, ExcelCategoryVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);

        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

    //分类状态修改
    @PutMapping("/changeStatus")
    public ResponseResult updateCategoryStatus(@RequestBody Category category ){

        return iCategoryService.updateCategoryStatus(category);
    }

}
